<template>
  <div class="risk">
    <van-search
      @search="onSearch"
      @clear="clearSearch"
      class="info_search input_text"
      v-model="value"
      placeholder="搜索"
    />
    <van-list
      style="margin: 10px"
      :finished="finished"
      v-model="loading"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-radio-group @change="handleChange" v-model="result">
        <van-cell
          style="margin-top: 10px; border-radius: 8px"
          v-for="(item, index) in list"
          :key="index"
        >
          <div class="warn_item">
            <div>
              <div class="warn_header">
                <div>{{ item.CompanyName }}</div>
              </div>
              <div class="dep_sort">
                <img src="@/assets/Images/AppIcon/contact.png" alt="" />
                <div>主要负责人：{{ item.Manager }}</div>
              </div>
              <div class="dep_sort">
                <img src="@/assets/Images/AppIcon/icon_list_phone.png" alt="" />
                <div>电话：{{ item.ManagerTel }}</div>
              </div>
              <van-radio :name="item.EnterpriseCode"></van-radio>
            </div>
          </div>
        </van-cell>
      </van-radio-group>
    </van-list>
  </div>
</template>

<script>
import { getConstructSafeCompanyPageData } from "@/api/public";
import { mapGetters } from "vuex";
export default {
  props: {
    CompanyCode: {
      type: String,
      required: false,
    },
  },
  data() {
    return {
      value: "",
      list: [],
      result: "",
      filter: {
        page: 0,
        companyCode: "",
      },
      loading: false,
      finished: false,
    };
  },
  created() {
    this.filter.companyCode = this.CompanyCode;
    this.getList();
  },
  computed: {
    ...mapGetters(["roles"]),
  },
  methods: {
    handleChange(id) {
      const newData = this.list.filter((item) => item.EnterpriseCode === id);
      if (newData.length > 0) {
        var item = {
          value: newData[0].EnterpriseCode,
          name: newData[0].CompanyName,
        };
        this.$emit("CallBack", item);
      }
    },
    onSearch() {
      var conditions = [];
      if (this.value) {
        conditions.push({
          Name: "Keywords",
          DisplayName: this.value,
          Value: this.value,
          Operator: "like",
          DataType: "String",
          IsTemp: false,
        });
      }
      this.filter.page = 0;
      this.filter.conditions = JSON.stringify(conditions);
      this.getList();
    },
    clearSearch() {
      this.filter = {
        page: 0,
        companyCode: this.companyCode,
      };
      this.value = "";
      this.getList();
    },

    getList() {
      this.loading = true;
      getConstructSafeCompanyPageData(this.filter).then((res) => {
        this.loading = false;
        if (!res.data.HasNextPage) {
          this.finished = true;
        }
        this.list = res.data.Data;
        this.filter.page = res.data.PageIndex;
      });
    },
    onLoad() {
      this.loading = true;
      ++this.filter.page;
      list(this.filter).then((res) => {
        this.loading = false;
        this.filter.page = res.data.PageIndex;
        this.list = this.list.concat(res.data.Data);
        if (!res.data.HasNextPage) {
          this.finished = true;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.risk {
  background-color: #e8f1f8;
  .info_search {
    padding: 10px;
    border-radius: 5px !important;
    .van-field__control {
      background-color: #ffffff !important;
    }
  }
}
.all_dep {
  background-color: #ffffff;
}
.van-cell {
  padding: 0 0;
}
.warn_item {
  padding: 10px 10px;
  background-color: #ffffff;
  /*margin-bottom: 10px;*/

  border-radius: 5px;
  position: relative;
}
.warn_header {
  display: flex;
  justify-content: space-between;
  position: relative;
  margin-bottom: 10px;
  .warn_tag {
    position: absolute;
    right: -10px;
    top: 0;
    border-radius: 26.64rem 0 0 26.64rem;
  }
}
.dep_sort {
  display: flex;
  align-items: center;
  margin-bottom: 5px;
  img {
    height: 15px;
    width: 15px;
    margin-right: 5px;
  }
}
.warn_item {
  .van-radio {
    position: absolute;
    right: 10px;
    top: 41px;
  }
}
</style>
